* Specify computer
local comp="davidbyrne10"

* Specify input and output directories
local datdir = "/Users/`comp'/Dropbox/Research/Billcap/Stata/Data/"
local figdir = "/Users/`comp'/Desktop/ReStatFigs/"
local paperdir= "/Users/`comp'/Dropbox/Research/Billcap/Writing_presenting/TellmeSomething/"
gen fevar=1

* Pre-treatment dummies
replace first_treatment_date=td(01oct2012) if first_treatment_date<td(01oct2012)
replace first_treatment_date=td(30mar2013) if first_treatment_date>td(30mar2013) & first_treatment_date<td(1apr2013)
replace first_treatment_date=td(01jun2013) if first_treatment_date>td(15apr2013)
gen first_treatment_month=mofd(first_treatment_date)
gen diff_month=read_month-first_treatment_month
gen Tmneg7=cond(diff_month==-7 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg6=cond(diff_month==-6 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg5=cond(diff_month==-5 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg4=cond(diff_month==-4 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg3=cond(diff_month==-3 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg2=cond(diff_month==-2 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg1=cond(diff_month==-1 & T!=. & first_treatment_date<td(01jun2013),1,0)

* Blocks for identifying pre-treatment Tm3neg, Tm2neg Tm1neg
gen dec=cond(read_month==tm(2012m12),1,0)
gen jan=cond(read_month==tm(2013m1),1,0)
gen feb=cond(read_month==tm(2013m2),1,0)
gen mar=cond(read_month==tm(2013m3),1,0)
sort account_number read_month
by account_number: egen tot_dec=total(dec)
by account_number: egen tot_jan=total(jan)
by account_number: egen tot_feb=total(feb)
by account_number: egen tot_mar=total(mar)

* Blocks: before October 2012, non-treated between dec, jan, feb
gen block=cond(read_month<tm(2012m12),1,0)
replace block=block+cond(tot_dec==1 & tot_jan==1 & tot_feb==1 & tot_mar==1 & (dec==1 | jan==1 | feb==1),1,0)
replace block=0 if T==1 | T==.
replace block=1 if Tmneg1==1 & T==0
replace block=1 if Tmneg2==1 & T==0
replace block=1 if Tmneg3==1 & T==0

local treat="Tm0 Tm1 Tm2 Tm3 Tm4 Tm5 Tm6 Tm7"
local controls="read_month2-read_month11"				
save `datdir'tempdat.dta, replace

local lev=95
qui parmby "areg dailykWh `treat' `controls', absorb(fevar) cluster(account_number)", label norestore level(`lev')
keep if _n<=8
gen zero=0
gen period=1 if parmseq==1
replace period=period[_n-1]+1 if parmseq >=2 & parmseq<=8
replace period=period[_n-8] if parmseq>=9				
sort parmseq
qui save `datdir'hetero_DB_month_time2_figs2.dta, replace


* Estimate pre-treatment trends in similar fashion
* First three pre-treatment months for Figure 3 in paper
* Focus on these because we have 3 pre-treatment months for
* both treatment waves 1 and 2
use `datdir'tempdat.dta, clear
local treat="Tmneg3 Tmneg2 Tmneg1"
local controls="read_month2-read_month11"

qui parmby "areg dailykWh `treat' `controls', absorb(fevar) cluster(account_number)", label norestore level(`lev')
qui keep if _n<=3
qui gen zero=0
qui gen period=-2 if parmseq==1
qui replace period=period[_n-1]+1 if parmseq >=2 & parmseq<=3
qui replace period=period[_n-3] if parmseq>=4					
qui replace parmseq=parmseq-100
qui sort parmseq
qui merge m:m parmseq using `datdir'hetero_DB_month_time2_figs2.dta, nogen
sort parmseq
qui replace parmseq=_n
qui drop if period==8
qui save `datdir'hetero_DB_month_time2_figs2.dta, replace


* Estimate pre-treatment trends in similar fashion for all pre-treatment periods
* For treatment wave 1: Sep, Aug, Jul
* For treatment wave 2: Feb, Jan, Dec, Nov, Oct, Sep, Aug, Jul
* First three pre-treatment months for Figure 3 in paper
* Focus on these because we have 3 pre-treatment months for
* both treatment waves 1 and 2
use `datdir'tempdat.dta, clear
local treat="Tmneg7 Tmneg6 Tmneg5 Tmneg4"
local controls="read_month2-read_month11"

qui parmby "areg dailykWh `treat' `controls', absorb(fevar) cluster(account_number)", label norestore level(`lev')
qui keep if _n<=4
qui gen zero=0
qui gen period=-6 if parmseq==1
qui replace period=period[_n-1]+1 if parmseq >=2 & parmseq<=4
qui replace period=period[_n-5] if parmseq>=5
qui replace parmseq=parmseq-100
qui sort parmseq
qui merge m:m parmseq using `datdir'hetero_DB_month_time2_figs2.dta
sort parmseq
qui replace parmseq=_n
qui save `datdir'hetero_DB_month_time2_figs2.dta, replace
